package org.ecsoya.fabric.builder;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;

import org.hyperledger.fabric.sdk.NetworkConfig;
import org.hyperledger.fabric.sdk.exception.NetworkConfigurationException;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import static org.apache.commons.lang3.ArrayUtils.isNotEmpty;

import static java.util.Objects.nonNull;

public class NetworkTest {

	static FileFilter filter = new FileFilter() {
		@Override
		public boolean accept(File pathname) {
			return pathname.isFile() && pathname.getName().endsWith(".yml");
		}
	};

	public static void main(String[] args) {

		File dir = new File("src/main/resources/network");
		if (nonNull(dir) && dir.exists()) {

			File[] connFiles = dir.listFiles(filter);
			if (isNotEmpty(connFiles)) {
				for (File file : connFiles) {
					try {
						NetworkConfig network = NetworkConfig.fromYamlFile(file);
						System.out.println("testing file [" + file.toString() + "]\n"
								+ ToStringBuilder.reflectionToString(network, ToStringStyle.JSON_STYLE));
					} catch (NetworkConfigurationException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}

}
